<?php

class Dealscombined_Crawlers_Wowcher_SiteProcessor extends Dealscombined_SiteProcessor_ProcessorAbstract {

	private function getProducDetails()
	{
		$xml = $this->getXML($this->_feedPath);

		foreach ($xml->merchant as $merchants) {
			//var_dump($merchants);
			foreach ($merchants->prod as $product) {
				$data = array(
					'merchant_name' => $merchants['name'],
					'merchant_id' => $merchants['id'],
					'merchant_product_id' => $product->pId,
					'network_product_id' => $product['id'],
					'product_name' => $product->text->name,
					'description' => $product->text->desc,
					'category_id' => $product->cat->awCatId,
					'category_name' => $product->cat->awCat,
					'merchant_category' => $product->cat->mCat,
					'deep_link' => $product->uri->awTrack,
					'image_url' => $product->uri->awImage,
					'search_price' => '',
					'currency' => $product->price['curr'],
					'delivery_cost' => $product->price->delivery,
					'merchant_deep_link' => $product->uri->mLink,
					'merchant_image_url' => $product->uri->mImage,
					'thumbnail_url' => $product->uri->awThumb,
					'brand_id' => '',
					'brand_name' => $product->brand->brandName,
					'commission_amount' => $product->comAmount,
					'commission_group' => $product->comGroup,
					'condition' => '',
					'delivery_time' => '',
					'price' => $product->price->buynow,
					'ean' => $product->ean,
					'in_stock' => $product['in_stock'],
					'is_hotpick' => '',
					'isbn' => $product->isbn,
					'is_for_sale' => '',
					'language' => '',
					'merchant_thumb_url' => '',
					'model_number' => '',
					'mpn' => '',
					'parent_product_id' => '',
					'pre_order' => $product['pre_order'],
					'product_type' => '',
					'promotional_text' => $product->text->promo,
					'rrp_price' => $product->price->rrp,
					'specifications' => $product->text->spec,
					'stock_quantity' => '',
					'store_price' => $product->price->store,
					'upc' => '',
					'valid_from' => $product->valFrom,
					'valid_to' => $product->valTo,
					'warranty' => $product->text->warranty,
					'web_offer' => $product['web_offer'],
				);

				try {
					$this->_database->insert('products_temp', $data);
				} catch(Exception $e) {
					echo 'Error with Db: ' . $e->getMessage() . PHP_EOL;
				}
			}
		}
	}

	public function process($userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)')
	{
		echo 'Starting Time for '.get_class($this).' Product Details:'. date("Y-m-d H:i:s") . PHP_EOL;
		$this->getProducDetails();
		echo 'Finishing Time: ' . date("Y-m-d H:i:s") . PHP_EOL;
	}
}
